<template>
  <div style="height: 270px; width: 100%">
    <div ref="basicCanvas" style="height: 100%; width: 100%"></div>
  </div>
</template>

<script>
import { resize } from "./resize"
export default {
  mixins: [resize],
  data() {
    return {
      myChart: null,
    };
  },
  props: {
    chartData: {
      type: Array,
      default: () => [[],[],[]],
    },
    title: {
      type: Array,
      default: () => ["", "", ""],
    },
    formatter: {
      type: String,
      default: "{c}"
    }
  },
  watch: {
    chartData: {
      handler(val){
        this.initChart(val);
      },
      deep: true,
    },
  },
  created(){
    this.initChart(this.chartData);
  },
  methods: {
    async initChart(val) {
      await this.$nextTick();

      if(this.myChart) { this.myChart.dispose() };
      this.myChart = this.$echarts.init(this.$refs.basicCanvas);

      // let lineMin = Math.min(...this.lineChartData),
          // lineMax = Math.max(...this.lineChartData);

      let option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
          // formatter: "{b} <br /> {a0}: {c0}" + "%" + "<br />{a1}: {c1}" + "%",
        },
        legend: {
          data: this.title,
          y: "bottom"
        },
        grid: [
          {
            x: "10px",
            x2: "10px",
            y2: "50px",
            height: "30%",
          },
          {
            x: "10px",
            x2: "10px",
            y: "10px",
            height: "50%",
          },
        ],
        xAxis: [
          {
            type: "category",
            gridIndex: 0,
            data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            axisLabel: {
              interval: 0,
            },
            textStyle: {
              fontSize: "16px",
            }
          },
          {
            type: "category",
            gridIndex: 1,
            data: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
            axisLabel: {
              show: false
            },            
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            }
          },
        ],
        yAxis: [
          {
            type: "value",
            gridIndex: 0,
            splitLine: { show: false }, //去除网格线
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLine: {
              show: false,
            },
          },
          {
            type: "value",
            gridIndex: 1,
            splitLine: { show: false }, //去除网格线
            axisLabel: {
              formatter: "{value}%",
              show: false,
            },
            axisTick: {
              //y轴刻度线
              show: false,
            },
            axisLine: {
              //y轴
              show: false,
            },
          }
        ],
        series: [
          {
            type: "bar",
            name: this.title[0],
            data: val[0],
            xAxisIndex: 0,
            yAxisIndex: 0,
            barWidth: 22,
            color: "rgb(180,199,231)",
            borderWidth: 1,
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "bottom",
                  textStyle: {
                    color: "#000",
                  },
                  formatter: this.formatter,
                  offset: [0, -20]
                },
              },
            },
          },
          {
            type: "line",
            name: this.title[1],
            smooth: true,
            xAxisIndex: 1,
            yAxisIndex: 1,
            data: val[1],
            color: "rgb(0,176,80)",
            lineStyle: {
              normal: {
                color: "rgb(0,176,80)", // 线条颜色
              },
            },
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "bottom",
                  textStyle: {
                    color: "rgb(0,176,80)",
                  },
                  formatter: "{c}",
                },
              },
            },
          },
          {
            type: "line",
            name: this.title[2],
            smooth: true,
            xAxisIndex: 1,
            yAxisIndex: 1,
            data: val[2],
            color: "#c00000",
            lineStyle: {
              normal: {
                color: "#c00000", // 线条颜色
              },
            },
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: "top",
                  textStyle: {
                    color: "#c00000",
                  },
                  formatter: "{c}",
                },
              },
            },
          },
        ],
      };
      this.myChart.setOption(option);
    },
  },
};
</script>

<style>
</style>
